package lijiale.project.market.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lijiale.project.market.model.MarketModel;
import lijiale.project.util.JDBCUtil;

public class MarketDao {

	public static Integer insert(MarketModel model) {
		String sql = "insert into market(code,name,addr,tel) values (?,,?,?,?) ";
		List<Object> vals = Arrays.asList(model.getCode(), model.getName(), model.getAddr(), 
				model.getTel());
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql, vals);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
	public static Integer deleteByCode(MarketModel model) {
		String sql = "delete from market where code=?";
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql, model.getCode());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
	public static Integer deleteByOther(MarketModel model) {
		StringBuffer sql = new StringBuffer("delete from market where 1=1");
		List<Object> vals = appendWhere(sql, model);
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql.toString(), vals);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
	public static Integer updateAll(MarketModel model) {
		String sql = "update market set name=? , addr=? ,tel=? where code=? ";
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql, model.getName(), model.getAddr(), model.getTel(),
					model.getCode());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
	public static Integer updateActive(MarketModel model) {
		StringBuffer sql = new StringBuffer("update market set id=id ");
		List<Object> vals = new ArrayList<>();
		String name = model.getName();
		if (name != null) {
			sql.append(",name=? ");
			vals.add(name);
		}
		String pass = model.getAddr();
		if (pass != null) {
			sql.append(",addr=? ");
			vals.add(pass);
		}
		String tel = model.getTel();
		if (tel != null) {
			sql.append(",tel =? ");
			vals.add(tel);
		}
		sql.append(" where code=? ");
		vals.add(model.getCode());
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql.toString(), vals);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
	public static List<MarketModel> select(MarketModel model) {
		StringBuffer sql = new StringBuffer("select *from market where 1=1 ");
		List<Object> vals = appendWhere(sql, model);
		Map<String, String> fields = new HashMap<>();
		fields.put("code", "code");
		fields.put("name", "name");
		fields.put("addr", "addr");
		fields.put("tel", "tel");
		List<MarketModel> list = new ArrayList<>();
		try {
			list = JDBCUtil.queryList(sql.toString(), vals, MarketModel.class, fields);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public static MarketModel selectModel(MarketModel model) {
		if (select(model).size() == 0) {// 判断如果没找到数据，则返回null
			return null;
		} else
			return select(model).get(0);
	}
	
	public static List<Object> appendWhere(StringBuffer sql, MarketModel model) {
		List<Object> vals = new ArrayList<>();
		String code = model.getCode();
		if (code != null && code.trim().isEmpty()) {
			sql.append(" and code like ? ");
			vals.add(code);
		}
		String name = model.getName();
		if (name != null && name.trim().isEmpty()) {
			sql.append(" and name like ? ");
			vals.add(name);
		}
		String pass = model.getAddr();
		if (pass != null && pass.trim().isEmpty()) {
			sql.append(" and addr like ? ");
			vals.add(pass);
		}
		String tel = model.getTel();
		if (tel != null && tel.trim().isEmpty()) {
			sql.append(" and tel like ? ");
			vals.add(tel);
		}
		return vals;
	}
	
}
